//#pragma comment(linker,"/STACK:16777216") /*16Mb*/
//#pragma comment(linker,"/STACK:33554432") /*32Mb*/
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<numeric>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<memory>
#include<string>
#include<vector>
#include<cctype>
#include<list>
//#include<queue>
//#include<deque>
//#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;

typedef unsigned long long      ui64;
typedef long long               i64;
typedef	vector<int>             VI;
typedef	vector<bool>            VB;
typedef	vector<VI>              VVI;
typedef	vector<string>          VS;
typedef	pair<int,int>           PII;
typedef map<string,int>         MSI;
typedef set<int>                SI;
typedef set<string>             SS;
typedef map<int,int>            MII;
typedef	pair<double,double>     PDD;

#define PB                      push_back
#define MP                      make_pair
#define X                       first
#define Y                       second
#define FOR(i, a, b)            for(int i = (a); i < (b); ++i)
#define RFOR(i, a, b)           for(int i = (a) - 1; i >= (b); --i)
#define CLEAR(a, b)             memset(a, b, sizeof(a))
#define SZ(a)                   int((a).size())
#define ALL(a)                  (a).begin(), (a).end()
#define RALL(a)                 (a).rbegin(), (a).rend()
#define INF                     (2000000000)

int MASK;
int used;
int sz[32];
PII G[32][600];

bool dfs(int v) {
	used|=(1<<v);
	if( v==1 ) return true;
	FOR(i,0,sz[v] ) {
		if( ((1<<G[v][i].Y) & MASK) && !(used&(1<<G[v][i].X)) )
			if( dfs( G[v][i].X ) )
				return true;
	}
	return false;
}


int bits[ 1<<20 ];
int USED[1<<20];
bool R[33][33][22];

int qu[100];

int main()
{
	int n,m,k;
	scanf("%d%d%d",&k,&n,&m);

	FOR(i,0,m) {
		int a, b, v;
		scanf("%d%d%d",&a,&b,&v);
		R[a][b][v] = R[b][a][v] = true;

	}

	FOR(i,0,n) {
		FOR(j,0,n)
			FOR(r,0,k)
				if( R[i][j][r] )
					G[i][sz[i]++] =  MP(j,1<<r);
	}
		
	int ans_mask = (1<<k)-1;
	int ans_k = k;
	USED[0] = 1;

	FOR(mask,1,(1<<k) ) {
		int K = 0;
		K = bits[mask] = bits[mask&(mask-1)]+1;
		if( K>=ans_k )
			continue;
		
		used = USED[mask&(mask-1)];
		int it1=0,it2=0;
		FOR(i,0,k) {
			if( (1<<i)&used ) {
				qu[it2++] = i;
			}
		}
		while( it1<it2 ) {
			int v = qu[it1++];
			FOR(i,0,sz[v]) {
				if( !((1<<G[v][i].X)&used) && (mask&G[v][i].Y) ) {
					used|=(1<<G[v][i].X);
					qu[it2++] = G[v][i].X;
				}
			}
		}
		USED[mask] = used;
		
		if( used & 2 ) {
			ans_mask = mask;
			ans_k = K;
		}
	}

	printf("%d\n",ans_k);
	FOR(i,0,k)
		if( (1<<i) & ans_mask)
			printf("%d ",i);
	printf("\n");
    return 0;
}